package com.github.wxiaoqi.security.biz.codex.api;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.auth.client.jwt.UserAuthUtil;
import com.github.wxiaoqi.security.auth.common.util.jwt.IJWTInfo;
import com.github.wxiaoqi.security.biz.codex.biz.MarActivityBiz;
import com.github.wxiaoqi.security.biz.codex.entity.MarActivity;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.rest.BaseController;
import com.github.wxiaoqi.security.common.util.Query;
import com.github.wxiaoqi.security.common.util.SerialNoUtil;

@Controller
@RequestMapping("activity")
public class ActivityApi extends BaseController<MarActivityBiz, MarActivity>{
	private final Logger logger = LoggerFactory.getLogger(ActivityApi.class);
	
	@Autowired
	private MarActivityBiz activityBiz;
    @Autowired
    private UserAuthUtil userAuthUtil;
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ObjectRestResponse<MarActivity> addActivityInfo(@RequestBody Map<String, Object> params) {
        logger.info("添加活动。。。{}"+params);
        MarActivity activity = new MarActivity();
		String authToken = (String) params.get("actToken");
		try {
			IJWTInfo info =	userAuthUtil.getInfoFromToken(authToken);
			activity.setCreateUser(info.getXId());
		} catch (Exception e) {
			logger.info("未获取到当前用户");
			e.printStackTrace();
		}
		String actInfo = (String) params.get("actInfo");
		String actStauts = (String) params.get("actStauts");
		String actRemark = (String) params.get("actRemark");
		activity.setActInfo(actInfo);
		activity.setActRemark(actRemark);
		activity.setActStauts(actStauts);
		activity.setCreateTime(new Timestamp(new Date().getTime()));
		activity.setActId(SerialNoUtil.createID());
		activityBiz.insertSelective(activity);
		return new ObjectRestResponse<MarActivity>();
	}
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ResponseBody
	public ObjectRestResponse<MarActivity> getMerCustInfo(@RequestBody Map<String, Object> params) {
		logger.info("获取单条活动信息。。。{}"+params);
		ObjectRestResponse<MarActivity> entityObjectRestResponse = new ObjectRestResponse<>();
		Integer seqNo = (Integer) params.get("seqNo");
		Long seqNoLong = Long.valueOf(String.valueOf(seqNo));
		MarActivity activity = activityBiz.selectById(seqNoLong);
        entityObjectRestResponse.data(activity);
        return entityObjectRestResponse;
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ObjectRestResponse<MarActivity> updateFeeInfo(@RequestBody Map<String, Object> params) {
		logger.info("修改活动信息。。。"+params);
		Integer seqNo = (Integer) params.get("seqNo");
		String actStauts = (String) params.get("actStauts");
		String actInfo = (String) params.get("actInfo");
		String actRemark = (String) params.get("actRemark");
		String actId = (String) params.get("actId");
		String createTime =  (String) params.get("createTime");
		String createUser = (String) params.get("createUser");
		if (!"01".equals(actStauts)) {
			// 若活动是未开始或者结束状态的时间，该活动下的所有红包积分都设置为不可领状态
			activityBiz.setStatus(actId);
		}
		MarActivity activity = new MarActivity();
		activity.setSeqNo(Long.valueOf(seqNo));
		activity.setActId(actId);
		activity.setCreateTime(Timestamp.valueOf(createTime));
		activity.setCreateUser(createUser);
		activity.setActInfo(actInfo);
		activity.setActStauts(actStauts);
		activity.setActRemark(actRemark);
		activityBiz.updateById(activity);
		return new ObjectRestResponse<MarActivity>();
	}
	

	 /**
	  * 获取红包领取率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:10
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getCouponRevRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCouponRevRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getCouponRevRateData(params);
	}
	/**
	  * 获取活动中红包领取率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:10
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityCouponRevRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityCouponRevRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityCouponRevRateData(params);
	}
	 /**
	  * 获取红包使用率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:12
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getCouponUseRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCouponUseRateData() {
		Map<String, Object> respMap = new HashMap<>();
		return activityBiz.getCouponUseRateData(respMap);
	}
	/**
	  * 获取活动中红包使用率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:12
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityCouponUseRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityCouponUseRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityCouponUseRateData(params);
	}
	 /**
	  * 获取红包场景占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:15
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getCouponSceneRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCouponSceneRateData() {
		Map<String, Object> params= new HashMap<>();
		return activityBiz.getCouponSceneRateData(params);
	}
	/**
	  * 获取活动中红包场景占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:15
	  * @param params
	  * @returngetIntegralUseRateData
	  */
	@RequestMapping(value = "/getActivityCouponSceneRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityCouponSceneRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityCouponSceneRateData(params);
	}
	 /**
	  * 获取积分领取率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:14
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getIntegralRevRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getIntegralRevRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getIntegralRevRateData(params);
	}
	/**
	  * 获取活动中积分领取率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:14
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityIntegralRevRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityIntegralRevRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityIntegralRevRateData(params);
	}
	 /**
	  * 获取积分使用率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:17
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getIntegralUseRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getIntegralUseRateData() {
		Map<String, Object> params= new HashMap<>();
		return activityBiz.getIntegralUseRateData(params);
	}
	/**
	  * 获取活动中积分使用率
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:17
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityIntegralUseRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityIntegralUseRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityIntegralUseRateData(params);
	}
	 /**
	  * 获取积分场景占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:18
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getIntegralSceneRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getIntegralSceneRateData() {
		Map<String, Object> params= new HashMap<>();
		return activityBiz.getIntegralSceneRateData(params);
	}
	/**
	  * 获取活动中积分场景占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:18
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityIntegralSceneRateData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityIntegralSceneRateData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityIntegralSceneRateData(params);
	}
	 /**
	  * 获取让利占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:22
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getAmountProportionData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getAmountProportionData() {
		Map<String, Object> params= new HashMap<>();
		return activityBiz.getAmountProportionData(params);
	}
	/**
	  * 获取活动中让利占比
	  * @author centerroot
	  * @time 创建时间:2018年2月24日下午2:16:22
	  * @param params
	  * @return
	  */
	@RequestMapping(value = "/getActivityAmountProportionData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getActivityAmountProportionData(@RequestBody Map<String, Object> params) {
		return activityBiz.getActivityAmountProportionData(params);
	}
	
	/**
	 * 获取活动列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	public TableResultResponse<MarActivity> getCouponList(@RequestParam("page") String page, @RequestParam("limit") String limit, String actInfo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		if(StringUtils.isNotBlank(actInfo)){
			params.put("actInfo", actInfo);
		}
		logger.info("查询活动列表。。。{}"+params);
		Query query = new Query(params);
		Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
		List<MarActivity> list = activityBiz.queryActivityList(params);
		return new TableResultResponse<MarActivity>(result.getTotal(), list);
	}
}
